/**
 * Created by Administrator on 2017/7/14.
 */
//注册js
$(document).on('click', '.register-btn', function () {
    var mobile = $("input[name='mobile']").val(), password = $("input[name='password']").val(), realname = $("input[name='realname']").val();
    var department_id = $("input[name='department_id']:checked").val();  //部门
    // console.log(realname);
    var duty_id = $("input[name='duty_id']:checked").val();// 职务
    var upuid = $("input[name='upuid']:checked").val(); //上级
    if (isChinaName(realname) == false) {
        layer.open({
            content: '姓名错误'
            , skin: 'msg'
            , time: 2 //2秒后自动关闭
        });
        return false;
    }
    if (isPhoneNo(mobile) == false) {
        layer.open({
            content: '手机号格式不正确'
            , skin: 'msg'
            , time: 2 //2秒后自动关闭
        });
        return false;
    }
    if (CheckPassWord3(password) == false) {
        layer.open({
            content: '密码只能为数字加字母长度最少6位'
            , skin: 'msg'
            , time: 2 //2秒后自动关闭
        });
        return false;
    }
    if (department_id == '' || department_id==undefined) {
        layer.open({
            content: '部门必须选择'
            , skin: 'msg'
            , time: 2 //2秒后自动关闭
        });
        return false;
    }
    if (duty_id == '' || duty_id==undefined) {
        layer.open({
            content: '职务必须'
            , skin: 'msg'
            , time: 2 //2秒后自动关闭
        });
        return false;
    }
    if (upuid == '' ||upuid==undefined) {
        upuid = 0;
    }
    var data = {
        mobile: mobile,
        password: password,
        department_id: department_id,
        duty_id: duty_id,
        upuid: upuid,
        realname: realname
    };
    var url = '/register.html', index;
    $.ajax({
        url: url,
        dataType: 'json',
        type: 'post',
        data: data,
        cache: false,
        async: true,
        beforeSend: function (XMLHttpRequest) {
            index = layer.open({
                type: 2,
                shadeClose: false,
                content: '提交中...'
            });

        },
        complete: function (XMLHttpRequest) { //请求完成时运行的函数（在请求成功或失败之后均调用，即在 success 和 error 函数之后）。
            layer.close(index);
        },
        success: function (json) {
            // console.log(json);
            if (json.code > 0) {
                //提示

                layer.open({
                    content: json.msg
                    , skin: 'msg'
                    , time: 2 //2秒后自动关闭
                });

                return true;
            } else if (json.code < 0) {


                layer.open({
                    content: json.msg
                    , skin: 'msg'
                    , time: 2 //2秒后自动关闭
                });
                // setTimeout(function () {
                //     window.location.reload();
                // }, 3000);
                return false;
            }
        },
        error: function () {
            layer.open({
                content: '请求失败'
                , skin: 'msg'
                , time: 2 //2秒后自动关闭
            });

        }
    });
    return false
});